package alfred.mdp.tsp.worker;

import alfred.mdp.tsp.Titanic;
import alfred.mdp.worker.WorkerService;
import alfred.storage.Storage;
import alfred.uuid.generator.UuidGenerator;
import org.zeromq.ZFrame;
import org.zeromq.ZMsg;

/**
 */
public class RequestWorker extends WorkerService {
    private final UuidGenerator<byte[]> uuidGenerator;
    private final Storage<ZFrame, ZMsg> storage;

    public RequestWorker(UuidGenerator<byte[]> uuidGenerator, Storage storage) {
        this.uuidGenerator = uuidGenerator;
        this.storage = storage;
    }

    @Override
    public ZMsg process(ZMsg input) {
        ZFrame uuid = new ZFrame(uuidGenerator.generate());

        ZMsg toStore = new ZMsg();
        toStore.addLast(Titanic.ResponseCode.PENDING);
        toStore.addAll(input);
        storage.put(uuid, toStore);

        ZMsg result = new ZMsg();
        result.addLast(Titanic.ResponseCode.OK);
        result.addLast(uuid);
        return result;
    }

    @Override
    public String getName() {
        return Titanic.TITANIC_REQUEST_SERVICE;
    }
}
